﻿using System;
using System.Collections.Generic;
using System.Linq;
using CSUtils;
using G02;
using G02.SceneConfig;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    /// <summary>
    /// 圆柱地形
    /// </summary>
    public class CCylinderTerrain : CSingleton<CCylinderTerrain> {
        const string _tag = "CCylinderTerrain";
        public CCircle circle = new CCircle();
        public CPara para = new CPara();
        Cfg_Scene[] sceneCfg;

        public void Init() {
            CMonoMonitor.UpdateEventAdd(_tag, 0.05f, "", _Update);
            circle.para.centerPoint = -Vector3.up * CCylinderTerrain.CPara.radius;
            sceneCfg = Cfg_Scene.arr.Where(n => n.id != 1).ToArray();
            startTerrainPath = Cfg_Scene.Get(1).onlyAssetsPath;
        }

        public void Open() {
            isRuning = true;
        }

        public void End() {
            isRuning = false;
        }

        string startTerrainPath;

        public void Reset() {
            currPos = Vector3.forward * 100;
            if (terrainList.Count > 0) {
                for (int i = 0; i < terrainList.Count; i++) {
                    terrainList[i].root.name += "_被丢弃";
                    deathList.Add(terrainList[i]);
                }
                terrainList.Clear();
            }

            var tf = C128_AssetsCore.GetModel(startTerrainPath).transform;
            tf.position = -Vector3.forward * 100;
            tf.eulerAngles = Vector3.zero;
            tf.SetActive(true);
            _AddTerrain(tf, startTerrainPath);
            useIndex = 0;
            CDelayMain.Delay(0.3f).SetPauseEnd("").onCallback = delegate {
                _Refresh();
            };
        }

        List<TerrainGo> terrainList = new List<TerrainGo>();
        List<TerrainGo> deathList = new List<TerrainGo>();

        bool isRuning;

        void _Update(float dd) {
            if (isRuning == false) {
                return;
            }
            _Refresh();
        }

        Vector3 currPos = Vector3.zero;
        int useIndex;

        void _Refresh() {
            while (terrainList.Count < para.showRegionNumber) {
                var pos = circle.ToCircleSurfacePos(currPos, Vector3.forward * para.length);
                //Debug.LogErrorFormat("______posi=_____{0}___", posi);
                Transform tf = null;
                string terrainPath = null;
                if (deathList.Count > 0) {
                    tf = deathList[0].root;
                    terrainPath = deathList[0].path;
                    deathList.RemoveAt(0);
                } else {
                    useIndex %= sceneCfg.Length;
                    terrainPath = sceneCfg[useIndex].onlyAssetsPath;
                    tf = C128_AssetsCore.GetModel(terrainPath, 2).transform;
                    useIndex++;
                }
                tf.position = currPos;

                tf.rotation = Quaternion.FromToRotation(Vector3.forward, pos - currPos);
                currPos = pos;
                _AddTerrain(tf, terrainPath);
                //  break;
            }
            //var direction0 = terrainList[0].position - circle.para.centerPoint;
            var firstPos = CGameCore.GetFirstHeroPos();
            var firstTran = terrainList[0];
            var pos2 = firstTran.root.position;
            var direction1 = circle.ToWorldDirection(pos2, firstPos - pos2);
            if (direction1.z > 325) {
                firstTran.root.name += "_被丢弃0";
                firstTran.root.SetActive(false);
                terrainList.RemoveAt(0);
                deathList.Add(firstTran);
            }
        }

        void _AddTerrain(Transform tf, string terrainPath) {
            tf.SetActive(true);
            tf.name = $"地形_{terrainList.Count}={terrainPath.ToStr()}";
            var tran = new TerrainGo(tf, terrainPath);
            terrainList.Add(tran);
        }

        public void AddToDeathList(Transform tf, string terrainPath = null) {
            if (string.IsNullOrEmpty(terrainPath)) {
                terrainPath = tf.name;
            }
            tf.gameObject.SetActive(false);
            var tran = new TerrainGo(tf, terrainPath);
            terrainList.Add(tran);
        }

        public class TerrainGo {
            public Transform root;
            public string path;

            public TerrainGo(Transform root, string path) {
                this.root = root;
                this.path = path;
            }
        }

        public class CPara {
            /// <summary>
            /// 单个地块的长度
            /// </summary>
            public float length = 100;
            public static float radius = 1000;
            public int showRegionNumber = 9;
            //public static Func<ITransform> onGetFrestRoleRoot;
        }
    }
}
